/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.technical.controller;

import com.fzhucc.annotation.Log;
import com.fzhucc.annotation.rest.AnonymousGetMapping;
import com.fzhucc.annotation.rest.AnonymousPostMapping;
import com.fzhucc.modules.security.domain.dto.AuthDto;
import com.fzhucc.modules.technical.domain.Technical;
import com.fzhucc.modules.technical.domain.dto.TechnicalAddDto;
import com.fzhucc.modules.technical.domain.dto.TechnicalDto;
import com.fzhucc.modules.technical.domain.dto.TechnicalEditDto;
import com.fzhucc.modules.technical.service.TechnicalService;
import com.fzhucc.modules.technical.domain.dto.TechnicalQueryCriteria;
import com.fzhucc.modules.thinktank.domain.dto.AcademicianDto;
import com.fzhucc.modules.thinktank.domain.dto.AllBindDto;
import com.fzhucc.modules.thinktank.domain.dto.AllBindDto2;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yyg
 * @date 2022-11-21
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "技术经理人:管理")
@RequestMapping("/api/technical")
public class TechnicalController {

    private final TechnicalService technicalService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('technical:list')")
    public void exportTechnical(HttpServletResponse response, TechnicalQueryCriteria criteria) throws IOException {
        technicalService.download(technicalService.queryAll(criteria), response);
    }


    @Log("查询技术经理人")
    @ApiOperation("查询技术经理人")
    @AnonymousGetMapping("/show")
    public ResponseEntity<Object> queryTechnical(TechnicalQueryCriteria criteria, Pageable pageable){
        criteria.setIsDelete(0);
        return new ResponseEntity<>(technicalService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增技术经理人")
    @ApiOperation("新增技术经理人")
    @PreAuthorize("@el.check('technical:add')")
    public ResponseEntity<Object> createTechnical(@Validated @RequestBody TechnicalAddDto resources){
        return new ResponseEntity<>(technicalService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改技术经理人")
    @ApiOperation("修改技术经理人")
    @PreAuthorize("@el.check('technical:edit')")
    public ResponseEntity<Object> updateTechnical(@Validated @RequestBody TechnicalEditDto resources){
        technicalService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除技术经理人")
    @ApiOperation("删除技术经理人")
    @PreAuthorize("@el.check('technical:del')")
    public ResponseEntity<Object> deleteTechnical(@RequestBody Integer[] ids) {
        technicalService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("生成技术经理人绑定小程序码")
    @Log("生成技术经理人绑定小程序码")
    @PostMapping(value = "/generateQrCode")
    @PreAuthorize("@el.check('technical:edit')")
    public AllBindDto generateQrCode(@RequestParam Integer technicalId) {
        return technicalService.generateQrCode(technicalId);
    }

    @ApiOperation("通过邀请码查询技术经理人")
    @Log("通过邀请码查询技术经理人")
    @AnonymousPostMapping(value = "/byCode")
    public TechnicalDto findByCode(@RequestParam String code) {
        return technicalService.findTechnicalByCode(code);
    }

    @ApiOperation("二维码绑定技术经理人")
    @Log("二维码绑定技术经理人")
    @AnonymousPostMapping(value = "/bind")
    public AuthDto bind(@RequestBody AllBindDto2 dto, HttpServletRequest request) {
        return technicalService.bindTechnical(dto, request);
    }
}